home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jedit_paren.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  4.8 KB  |  139 lines

  1. # jedit_paren.tcl - support for matching brackets in jedit
  2. #
  3. # These procedures are by David Svoboda <svoboda@ece.cmu.edu>, from the
  4. # Beth editor (the predecessor of Elsbeth).  They were borrowed and
  5. # modified by Christian Artigues <artigues@ensta.fr> and Maurice
  6. # Diamantini <diam@ensta.fr> for the STEAD editor (based on jedit), and
  7. # I'm incorporating them back into jedit (somewhat modified), with David's
  8. # permission.
  9. # I don't have the original copyright information for Beth, but
  10. # Elsbeth's copyright is:
  11. #
  12. #   Elsbeth & Teacher Hypertools: Copyright (c) 1994  David Svoboda
  13. # (and this code is very similar to the code in the current version of
  14. # Elsbeth).  There's an Elsbeth home page at
  15. #
  16. # http://www.ece.cmu.edu/afs/ece/usr/svoboda/www/elsbeth/homepage.html
  17.  
  18. # TO DO:
  19. # * make searching for matches much more efficient (what if you're at
  20. #   the end of a 1Mb document?
  21. # * maybe these procedures should be merged into the bindings libraries
  22.  
  23. ######################################################################
  24. # jedit:balance_bind t -
  25. #   set up bindings to balance parentheses etc. in $t
  26. #   (this procedure is from STEAD)
  27. ######################################################################
  28.  
  29. proc jedit:balance_bind { t } {
  30.   global jedit_balance_list
  31.   
  32.   set jedit_balance_list \
  33.     {{\( \) parenright} {\[ \] bracketright} {\{ \} braceright}}
  34.   
  35.   foreach pair $jedit_balance_list {
  36.     set left_char [lindex $pair 0]
  37.     set right_char [lindex $pair 1]        ;# unused
  38.     set right_keysym [lindex $pair 2]
  39.     bind $t <$right_keysym> \
  40.       "j:tkb:self_insert %W %K %A;
  41.       jedit:paren:flash_left_paren %W \\$left_char %A"
  42.   }
  43. }
  44.  
  45. ######################################################################
  46. # jedit:paren:find_left_paren t left right close_trace -
  47. #   return index of matching left partner, or "" if unsuccessful.
  48. #   (this procedure is from Beth)
  49. ######################################################################
  50.  
  51. proc jedit:paren:find_left_paren {t left right close_trace} {
  52.   set left [string trimleft $left \\]
  53.   set right [string trimleft $right \\]
  54.   # set close_trace [$t index "$index -1 chars"]
  55.   set open_trace $close_trace
  56.   while (1) {
  57.     update
  58.     # go back 1 left, quit if none found.
  59.     set backset [string last $left [$t get 1.0 $open_trace]]
  60.     if {($backset < 0)} {return ""}
  61.     set open_trace [$t index "1.0 +$backset chars"]
  62.     # go back 1 right, if none after open, return current open
  63.     set offset [string last $right [$t get \
  64.       $open_trace $close_trace]]
  65.     if {($offset < 0)} {return $open_trace}
  66.     set close_trace [$t index "$open_trace +$offset chars"]
  67.   }
  68. }
  69.  
  70. ######################################################################
  71. # jedit:paren:char_count t c start end -
  72. #   counts instances of $c between $start and $end in $t
  73. #   (this procedure is from Beth)
  74. ######################################################################
  75.  
  76. proc jedit:paren:char_count {t c start end} {
  77.   set offset 0
  78.   set count 0
  79.   set c [string trimleft $c \\]
  80.   
  81.   while {[set offset [string first $c [$t get $start $end]]] >= 0} {
  82.     incr count
  83.     set start [$t index "$start +$offset chars +1 chars"]
  84.   }
  85.   return $count
  86. }
  87.  
  88. ######################################################################
  89. # jedit:paren:balance_count t left right start end -
  90. #   checks if $left and $right occur same # of times in [$start $end] of $t
  91. #   (this procedure is from Beth)
  92. ######################################################################
  93.  
  94. proc jedit:paren:balance_count {t left right start end} {
  95.   set c1 [jedit:paren:char_count $t $left $start $end]
  96.   set c2 [jedit:paren:char_count $t $right $start $end]
  97.   
  98.   if {($c1 > $c2)} {return "[string trimleft $left \\] [expr $c1-$c2]"}
  99.   if {($c2 > $c1)} {return "[string trimleft $right \\] [expr $c2-$c1]"}
  100.   return ""
  101. }
  102.  
  103. ######################################################################
  104. # jedit:paren:flash_left_paren t left right
  105. #   flash left partner of character $left in text $t, or beep if none
  106. #   (this procedure is from STEAD)
  107. ######################################################################
  108.  
  109. proc jedit:paren:flash_left_paren {t left right} {
  110.   set result [jedit:paren:find_left_paren \
  111.     $t $left $right [$t index "insert -1 chars"]]
  112.   if {($result == "")} {        ;# not found
  113.     j:beep $t
  114.   } else {
  115.     jedit:flash $t $result "$result +1 chars"
  116.   }
  117. }
  118.  
  119. ######################################################################
  120. # jedit:flash t from to - flash a range briefly
  121. ######################################################################
  122.  
  123. proc jedit:flash {t from to} {
  124.   global JEDIT_PREFS
  125.   
  126.   set bg $JEDIT_PREFS(textbg)
  127.   set fg $JEDIT_PREFS(textfg)
  128.   
  129.   $t tag configure jedit_paren_match -background $fg -foreground $bg
  130.   $t tag raise jedit_paren_match
  131.   
  132.   $t tag add jedit_paren_match $from $to
  133.   update
  134.   
  135.   after 200 "$t tag delete jedit_paren_match"
  136. }
  137.